/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 17655
 * Date: 2025-03-11
 * Time: 14:07
 */
public class test2 {
    /**
     * LeetCode  238  除自身之外数组的乘积
     * @param nums
     * @return
     */

    public int[] productExceptSelf(int[] nums) {
        int n = nums.length;
        // 记录当前位置之前的乘积
        int[] f = new int[n];
        // 记录当前位置之后的乘积
        int[] g = new int[n];
        int[] ret = new int[n];
        f[0] = 1;
        g[n-1] = 1;
        for(int i=1;i<n;i++){
            f[i] = nums[i-1] * f[i-1];
        }
        for(int i=n-2;i>=0;i--){
            g[i] = nums[i+1]*g[i+1];
        }
        for(int i =0;i<n;i++){
            ret[i] = f[i] * g[i];
        }
        return ret;
    }
}
